package controllers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"math"
	"time"
	"zbn-oil-consumption/models"
	"zbn-oil-consumption/utils"
)

type Car struct {
}

var mCar models.Car

type CarInfo struct {
	CarInfo            models.Car    `json:"carInfo"`
	LatestRecord       models.Record `json:"latestRecord"`
	MonthlyStats       string        `json:"monthlyStats"`
	CurrentMileage     int           `json:"currentMileage"`
	AvgConsumption     float64       `json:"avgConsumption"`
	CurrentMonthOil    int           `json:"currentMonthOil"`
	CurrentMonthAmount int           `json:"currentMonthAmount"`
}

// Info 车辆信息
func (Car) Info(c *gin.Context) {
	userId := c.GetInt64("userId")
	res, err := mCar.FindByUserId(userId)
	if err != nil {
		utils.Fail(c, 0, "")
		return
	}
	utils.Success(c, 200, "success", res)
}

func (Car) Index(c *gin.Context) {
	userId := c.GetInt64("userId")
	res, err := mCar.FindByUserId(userId)
	if err != nil {
		utils.Fail(c, 0, "")
		return
	}
	latest, err := mRecord.FindLastByUserId(userId)
	totalOilVolume, err := mRecord.TotalByUserId(userId, "oil_volume")
	maxMileage, err := mRecord.MaxByUserId(userId, "mileage")
	minMileage, err := mRecord.MinByUserId(userId, "mileage")

	currentMonthOil, err := mRecord.TotalByUserIdCurrentMonth(userId, "oil_volume")
	currentMonthAmount, err := mRecord.TotalByUserIdCurrentMonth(userId, "amount")

	currentMileage := res.InitialMileage
	if maxMileage != nil {
		m := int(maxMileage.(int64))
		if m > currentMileage {
			currentMileage = m
		}
	}
	var avgConsumption float64
	if minMileage != nil {
		m := int(minMileage.(int64))
		cc := currentMileage - m
		if cc != 0 {
			fmt.Println(totalOilVolume)
			fmt.Println(cc)
			avgConsumption = float64(totalOilVolume*100) / float64(cc)
			avgConsumption = math.Round(avgConsumption*10) / 10
		}
	}

	result := CarInfo{
		CarInfo:            res,
		MonthlyStats:       "hao",
		LatestRecord:       latest,
		CurrentMileage:     currentMileage,
		AvgConsumption:     avgConsumption,
		CurrentMonthOil:    currentMonthOil,
		CurrentMonthAmount: currentMonthAmount,
	}
	utils.Success(c, 200, "success", result)
}

// Save 保存车辆
func (Car) Save(c *gin.Context) {
	var d models.Car
	if err := c.ShouldBindJSON(&d); err != nil {
		utils.Fail(c, 0, "参数错误")
		return
	}
	userId := c.GetInt64("userId")
	if userId == 0 {
		utils.Fail(c, 0, "请重新登录")
		return
	}
	d.UserId = userId
	exist, _ := mCar.ExistByUserId(userId)
	if exist {
		res, err := mCar.Update(d)
		if err != nil {
			utils.Fail(c, 0, "")
			return
		}
		utils.Success(c, 200, "", res)
	} else {
		d.CreatedAt = time.Now()
		res, err := mCar.Add(d)
		if err != nil {
			utils.Fail(c, 0, "")
			return
		}
		utils.Success(c, 200, "", res)
	}
}

// List 获取列表
func (Car) List(c *gin.Context) {
	var params utils.Params
	if err := c.ShouldBind(&params); err != nil {
		utils.Fail(c, 0, "参数错误")
		return
	}
	total, res, err := mCar.GetList(params)
	if err != nil {
		utils.Fail(c, 0, err.Error())
		return
	}
	utils.SuccessPagination(c, 200, "success", res, total, params.Page, params.PageSize)
}
